<template>
  <div>
    <table>
      <tr>
        <td><span style="color: red">*</span>仓库编号</td>
        <td><el-input v-model="storeHome.storeHomeCode"></el-input></td>
      </tr>
      <br />
      <tr>
        <td><span style="color: red">*</span>仓库名称</td>
        <td><el-input v-model="storeHome.storeHomeName"></el-input></td>
      </tr>
      <br />
      <tr>
        <td>仓管</td>
        <td>
          <el-select v-model="storeHome.employeeId">
            <el-option label="无" value=""></el-option>
            <el-option
              v-for="employee in employee"
              :key="employee.employeeId"
              :label="employee.employeeName"
              :value="employee.employeeId"
            >
            </el-option>
          </el-select>
        </td>
      </tr>
      <br />
    </table>
  </div>
</template>

<script>
export default {
  name: "AddStoreHome",
  data() {
    return {
      path: "",
      storeHome: {
        storeHomeCode: "",
        storeHomeName: "",
        employeeId: "",
        shId:'',
      },
      employee:[],
      oldSH:[],
      isVaName:false,
      isVaCode:false,
    };
  },
  methods: {
    letNull() {
      for (const key in this.storeHome) {
        if (this.storeHome.hasOwnProperty(key)) {
          this.storeHome[key] = '';
        }
      }
      this.oldSH = [];

    },
    addAndUp: async function () {
      const storeHomeName = this.storeHome.storeHomeName;
      const storeHomeCode = this.storeHome.storeHomeCode;
      const employeeId = this.storeHome.employeeId;
      const shId = this.storeHome.shId;

      if (!storeHomeCode) {
        this.$message({
          showClose: true,
          message: "仓库编码不可以为空",
          type: "error",
        });
        return false;
      }

      await this.vaCode();

      if (shId && storeHomeCode == this.oldSH.storeHomeCode) {
      } else if (this.isVaCode) {
        this.$message({
          showClose: true,
          message: "仓库编码重复",
          type: "error",
        });
        return false;
      }

      if (!storeHomeName) {
        this.$message({
          showClose: true,
          message: "仓库名称不可以为空",
          type: "error",
        });
        return false;
      }

      await this.vaName();

      if (shId && storeHomeName == this.oldSH.storeHomeName) {
      } else if (this.isVaName) {
        this.$message({
          showClose: true,
          message: "仓库名称重复",
          type: "error",
        });
        return false;
      }

      if (!shId) {
        try {
          const rs = await this.axios({
            url: "/info/storeHouse/insert",
            method: "POST",
            data: {
              storeHomeCode,
              storeHomeName,
              employeeId,
            },
          });
          if (rs.data.code === 200) {
            this.$message({
              showClose: true,
              message: "添加成功",
              type: "success",
            });

            this.letNull();
            return true;
          } else {
            this.$message({
              showClose: true,
              message: "添加失败",
              type: "error",
            });

            return false;
          }
        } catch (error) {
          this.$message({
            showClose: true,
            message: "添加失败,请求失败",
            type: "error",
          });
          return false;
        }
      } else {
        try {
          const rs = await this.axios({
            url: "/info/storeHouse/update",
            method: "POST",
            data: {
              storeHomeCode,
              storeHomeName,
              employeeId,
              shId,
            },
          });

          if (rs.data.code === 200) {
            this.$message({
              showClose: true,
              message: "修改成功",
              type: "success",
            });
            this.letNull();

            return true;
          } else {
            this.$message({
              showClose: true,
              message: "修改失败",
              type: "error",
            });

            return false;
          }
        } catch (error) {
          console.error("请求失败:", error);
          return false;
        }
      }
    },
    selectById(id) {

      this.axios({
        url: "/info/storeHouse/findById",
        params: {
          shId: id,
        },
      }).then((rs) => {
        this.oldSH = rs.data.storeHouse;
        this.storeHome=rs.data.storeHouse;
      });
    },
    vaCode: async function () {
      await this.axios({
        url: "/info/storeHouse/selectCount",
        params: {
          storeHomeCode: this.storeHome.storeHomeCode,
        },
      }).then((rs) => {
        if (rs.data.count > 0) {
          this.isVaCode = true;
        } else {
          this.isVaCode = false;
        }
      });
    },
    vaName: async function () {
      await this.axios({
        url: "/info/storeHouse/selectCount",
        params: {
          storeHomeName: this.storeHome.storeHomeName,
        },
      }).then((rs) => {
        if (rs.data.count > 0) {
          this.isVaName = true;
        } else {
          this.isVaName = false;
        }
      });
    },
    selectEm() {
      
      this.axios({
        url: "/info/employee/listAll",
      }).then((rs) => {
        this.employee=rs.data.list
      });
    },
  },
  created() {
    this.selectEm();
  },
};
</script>

<style scoped>
table td :not([id="contact"]) {
  text-align: left;
  padding-right: 10px;
  /* padding-top: 10px;
  padding-bottom: 10px; */
}

td:nth-child(odd) {
  text-align: center;
  padding-right: 10px;
}
</style>